<template>
  <div class="login_view">
    <div class="login_container">
      <div class="avatar_box">
        <img :src="logo" />
      </div>
      <el-form label-width="60px" class="login_form" :model="registerUser" :rules="rules" ref="registerUser">
        <el-form-item label="账号" prop="account">
          <el-input v-model="registerUser.account"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="registerUser.password" type="password"></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="userName">
          <el-input v-model="registerUser.userName"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="registerUser.email"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="Register">注册</el-button>
          <el-button @click="reset">重置</el-button>
          <el-button type="primary" @click="toLogin">去登陆</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>



import {doAddUser} from "@/api/user";
import logo from "@/assets/logo.png";

export default{
  data() {
    return {
      registerUser: {
        account: '',
        password: '',
        userName:'',
        userType:1,
        email:''
      },
      logo:logo,
      rules: {
        account: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 15, message: '用户名长度为3到15个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 5, max: 15, message: '密码长度为5到15个字符', trigger: 'blur' }
        ],
        userName: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
          { min: 3, max: 15, message: '用户名长度为3到15个字符', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          {pattern:/^([0-9A-Za-z\-_\.]+)@([0-9a-z]+\.[a-z]{2,3}(\.[a-z]{2})?)$/g,
        message: "请输入正确的邮箱,以@qq.com结尾",
        trigger: "blur"}
        ]
      }
    }
  },
  methods: {
    Register() {
      this.$refs.registerUser.validate((valid) => {
        if (valid){
          doAddUser(this.registerUser).then(res => {
            if (res.code === 200) {
              this.$message.success("注册成功")
              this.$router.push("/");
            }
          })
        }
      })
    },
    reset() {
      this.$refs.registerUser.resetFields();
    },
    toLogin(){
      this.$router.push('/')
    }
  }
}
</script>

<style lang="less" scoped>
.login_view {
  background-color: #2c3e50;
  height: 100%;
  .login_container {
    position: absolute;
    width: 470px;
    height: 420px;
    border-radius: 5px;
    left: 50%;
    top: 50%;
    -webkit-transform: translate(-50%, -50%);
    background-color: #fff;
    border: 1px solid #fff;
  }
  .avatar_box {
    height: 130px;
    width: 130px;
    border-radius: 50%;
    box-shadow: 0 0 10px #ddd;
    border: 1px solid #eee;
    background-color: #fff;
    position: absolute;
    padding: 10px;
    left: 50%;
    transform: translate(-50%, -50%);
    img {
      height: 100%;
      width: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
  .login_form {
    position: absolute;
    margin-top: 90px;
    width: 95%;
    box-sizing: border-box;
    .el-form-item {
      height: 50px;
    }
    .el-button{
      width: 25%;
      height: 80%;
    }
  }
}
</style>